<?php
class Wap_Models_Main extends Zend_Db_Table_Abstract
{
	private $db;
	function __construct()
	{
		parent::__construct();
		$this->db=Zend_Registry::get('connectDB');
	}
	
	
	public function listAll($table){
		$select = $this->db	->select()
							->from($table);
		$result = $this->db->fetchAll($select);
		return $result;		
	}
	//author : quy
	public function listAllQuerySelect($table){
		$select = $this->db	->select()
		->from($table);
		//$result = $this->db->fetchAll($select);
		return $select;
	}
	
	public function listbyOderLimit($table,$orderCol,$typeOder,$limit){
		$select = $this->db	->select()
							->from($table)
							->order("$orderCol $typeOder")
							->limit($limit);
		$result = $this->db->fetchAll($select);
		return $result;		
	}
	
	public function findByID($table,$colum,$id){
		$select = $this->db	->select()
							->from($table)
							->where("$colum = ?",$id);
		$result = $this->db->fetchRow($select);
		return $result;
	}
	public function findByIDfetchAll($table,$colum,$id){
		$select = $this->db	->select()
							->from($table)
							->where("$colum = ?",$id);
		$result = $this->db->fetchAll($select);
		return $result;
	}
	//trả về giá trị của field đầu tiên trong kết quả truy vấn
	public function findByIDReturnOneResult($table,$colum,$id){
		$select = $this->db	->select()
		->from($table)
		->where("$colum = ?",$id);
		$result = $this->db->fetchCol($select);
		return $result;
	}
	//trả về giá trị của field thứ 2 trong kết quả truy vấn
	public function findByIDReturnSecondResult($table,$colum,$id){
		$select = $this->db	->select()
		->from($table)
		->where("$colum = ?",$id);
		$result = $this->db->fetchCol($select);
		return $result;
	}
	//quote() là phương thức sử để sửa các dấu nháy đơn (‘) thành ( ’) trước khi đưa vào câu truy vấn Select.
	public function findByIDReturnQuoteResult($table,$colum,$value){
		$value = $db->quote($value,STRING);
		$select = $this->db	->select()
		->from($table)
		->where("$colum = ?",$value);
		$result = $this->db->fetchAll($select);
		return $result;
	}
	
	
	///insert
	public function insertTable($table, $data){
		$result = $this->db->insert($table, $data);
		return $result;
	}
	public function insertTableReturnLastID($table, $data){
		$result = $this->db->insert($table, $data);
		$id = $this->db->lastInsertId();
		return $id;
	} 
	
	//edit
	public function editTable($table,$data,$colum,$id)
	{
		$where="$colum = '$id'";
		return $this->db->update($table, $data, $where);
	}
	//edit mutil colum by id
	public function editMutilTable($table,$data,$colum,$list_id)
	{	
		//$where="$colum = ".$id;
		$where= $colum." IN (".$list_id.")";
		return $this->db->update($table, $data, $where);
	}
	
	public function delTable($table,$colum,$id)
	{
		$where="$colum = ".$id;
		$result =  $this->db->delete($table, $where);
		return $result;
	}
	
	public function delMutilTable($table,$colum,$list_id)
	{
		$where= $colum." IN (".$list_id.")";
		$result =  $this->db->delete($table, $where);
		return $result;
	}
	
	public function editSetting($data)
	{
		return $this->db->update('n_setting', $data);
	}
	public function checkExit($table,$colum,$value,$where=''){
		$select = $this->db	->select()
							->from($table)
							->where("$colum = ?",$value)
							->where($where);
		$result = $this->db->fetchAll($select);
		return count($result);
	}
	public function checkExit_3($table,$colum,$value){
		$select = $this->db	->select()
							->from($table)
							->where("$colum = ?",$value);
		$result = $this->db->fetchAll($select);
		return count($result);
	}
	
	public function selectTableByValue($table,$colum,$value){
		$select = $this->db	->select()
		->from($table)
		->where("$colum = ?",$value);
		$result = $this->db->fetchAll($select);
		return $result;
	}
	
	//Call library
	// alias title before insert
	public function filterAliasTitle($table, $colum, $str){
		$filter = new Zendvn_Filter_RemoveCircumflex($str);
		//call filter
		$result = $filter->filter($str);
		$result = str_replace(" ","-", $result);
		
		//check exist title in colum
		if($this->findByID($table, $colum, $result)>0){
			$result = $result."-".time();
		}		
		return $result;
	}

}